﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Web.Filters;
using CJAtech.BBS.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Base.Cache;
using Bigger.Web.Mvc;
using CJAtech.BBS.Process;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Controllers
{
    [BBSCorsFilter]
    [BBSAuthorFilter]
    [Obsolete("旧版论坛数据基类，新版请使用BBSBaseController")]
    public abstract partial class BBSController : BaseController
    {
        public CommunityConfig CurrentCommunityConfig
        {
            get
            {
                return HttpContext.GetItems("CommunityConfig") as CommunityConfig;
                //return ViewBag.CommunityConfig as CommunityConfig;
            }
        }

        public virtual ActionResult GetCurrentUserInfo()
        {
            var json = new { UserName = this.CurrentUserInfo.UserName, Avatar = this.CurrentUserInfo.Avatar };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        //0716
        public abstract UserInfo GetUserInfo();
        public UserInfo CurrentUserInfo
        {
            get
            {
                return GetUserInfo();
            }
        }

        [NonAction]
        public virtual ViewResult BBSView()
        {
            return View(string.Format("~/Views/{0}/{1}.cshtml", this.CurrentCommunityConfig.ConfigCode, this.RouteData.Values["action"]));
        }

        [NonAction]
        public virtual string CheckViewExist()
        {
            string viewName = this.RouteData.Values["ViewName"].ToString();
            if (viewName.IsNull())
            {
                SystemWebAsset.CreateHttp404Result();
            }
            string[] viewLocationFormats = new string[] 
            { 
                "~/Views/{1}/{0}.cshtml", 
                "~/Views/{1}/{0}.vbhtml", 
                "~/Views/Shared/{0}.cshtml", 
                "~/Views/Shared/{0}.vbhtml" 
            };
            //string controllerName = this.RouteData.Values["Controller"].ToString();
            List<string> viewLocations = new List<string>();
            bool flag = false;
            Array.ForEach(viewLocationFormats, format => viewLocations.Add(string.Format(format, viewName, this.CurrentCommunityConfig.ConfigCode)));
            string viewResult = string.Empty;
            foreach (string viewLocation in viewLocations)
            {
                string filePath = Request.MapPath(viewLocation);
                if (System.IO.File.Exists(filePath))
                {
                    flag = true;
                    viewResult = viewLocation;
                    break;
                }
            }
            if (!flag)
            {
                SystemWebAsset.CreateHttp404Result();
            }
            return viewResult;
        }
    }
}
